﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    ViewData["Title"] = "新增开票";
}

<div class="layui-container" style="padding:20px">
    <div class="layui-row">
        <div class="layui-col-md12">
            <h2>新增开票管理</h2>
        </div>
    </div>
    <div class="layui-row">
        <div class="layui-col-md8 layui-col-md-offset2">
            <form class="layui-form" id="AddOpenFare">
                <div class="layui-form-item">
                    <label >开票编号</label>
                    <div class="layui-input-block">
                        <input type="text" name="OpenFareCode" lay-verify="required" placeholder="请输入开票编号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label>开票主题</label>
                    <div class="layui-input-block">
                        <input type="text" name="OpenFareTheme" lay-verify="required" placeholder="请输入开票主题" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label>发票类型</label>
                    <div class="layui-input-block">
                        <input type="text" name="InvoiceTypeName" lay-verify="required" placeholder="请输入发票类型" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label>开票日期</label>
                    <div class="layui-input-block">
                        <input type="date" name="OpenFareTime" lay-verify="required" placeholder="请输入开票日期" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label>收票方</label>
                    <div class="layui-input-block">
                        <input type="text" name="CollectTickets" lay-verify="required" placeholder="请输入收票方" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label>开票金额</label>
                    <div class="layui-input-block">
                        <input type="text" name="OpenFareMoney" lay-verify="required" placeholder="请输入开票金额" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label>发票号</label>
                    <div class="layui-input-block">
                        <input type="text" name="InvoiceTypeCode" lay-verify="required" placeholder="请输入发票号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label>备注</label>
                    <div class="layui-input-block">
                        <input type="text" name="OpenFareRemark" placeholder="请输入备注" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="AddOpenFare">
                            <i class="layui-icon layui-icon-ok"></i> 提交
                        </button>
                        <button type="reset" class="layui-btn layui-btn-primary">
                            <i class="layui-icon layui-icon-refresh"></i> 重置
                        </button>
                        <button type="button" class="layui-btn layui-btn-warm" id="backToList">
                            <i class="layui-icon layui-icon-left"></i> 返回列表
                        </button>

                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


@section Scripts {
    <script>
        // 绑定表单提交与返回按钮（确保在 jQuery 之后执行）
        $(function(){
            $('#AddOpenFare').on('submit', function(e){
                e.preventDefault();
                AddOpenFare();
            });
            $('#backToList').on('click', function(){
                window.location.href = '/OpenFare/ShowOpenFare';
            });
        });

        // 统一消息提示，若未加载 layui 则回退到 alert
        function notify(message){
            if (window.layui && layui.layer){
                layui.layer.msg(message);
            } else {
                alert(message);
            }
        }

        function AddOpenFare() {
            var obj = {};
            obj.openFareCode = $("input[name='OpenFareCode']").val();
            obj.openFareTheme = $("input[name='OpenFareTheme']").val();
            obj.invoiceTypeName = $("input[name='InvoiceTypeName']").val();
            obj.openFareTime = $("input[name='OpenFareTime']").val();
            obj.collectTickets = $("input[name='CollectTickets']").val();
            obj.openFareMoney = $("input[name='OpenFareMoney']").val(); // 后端期望字符串
            obj.invoiceTypeCode = $("input[name='InvoiceTypeCode']").val();
            obj.openFareRemark = $("input[name='OpenFareRemark']").val();

            // 验证必填字段
            if (!obj.openFareCode || !obj.openFareTheme || !obj.invoiceTypeName || 
                !obj.openFareTime || !obj.collectTickets || !obj.openFareMoney || !obj.invoiceTypeCode) {
                notify("请填写所有必填字段");
                return;
            }

            $.ajax({
                url: 'http://localhost:5054/api/OpenFare/AddOpenFare',
                dataType: 'json',
                type: 'post',
                data: JSON.stringify({
                    OpenFareCode: obj.openFareCode,
                    OpenFareTheme: obj.openFareTheme,
                    InvoiceTypeName: obj.invoiceTypeName,
                    OpenFareTime: obj.openFareTime,
                    CollectTickets: obj.collectTickets,
                    OpenFareMoney: obj.openFareMoney,
                    InvoiceTypeCode: obj.invoiceTypeCode,
                    OpenFareRemark: obj.openFareRemark
                }),
                async: false,
                contentType: 'application/json',
                success: function(res) {
                    if (res && (res.code === 200 || res.Code === 200)) {
                        notify("开票管理添加成功！");
                        location.href = "/OpenFare/ShowOpenFare";
                    } else {
                        notify("开票管理添加失败：" + (res && (res.msg || res.Message) || "未知错误"));
                    }
                },
                error: function(xhr, status, error) {
                    var detail = (xhr.responseJSON && (xhr.responseJSON.title || JSON.stringify(xhr.responseJSON.errors))) || error;
                    notify("请求失败：" + detail);
                }
            });
        }
    </script>
}
